package api
{
	import UIUtils.uif_json;
	
	import api_servers.api_myserver;
	
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	import flash.utils.ByteArray;

	public class clt_myserver extends api_myserver {
		public static var MODEL_NAME: String = 'clt30';
		public static const SAFEREG: RegExp = /[;\\\}\{\[\]"\']/g;
		
		public function clt_myserver(a_sroot:Object, api_url:String, a_api_id:String, a_secret:String) {
			if (a_sroot.flashVars.modelName) MODEL_NAME = a_sroot.flashVars.modelName;
			super(a_sroot, api_url, a_api_id, a_secret);
		}
		
		public function oneQuery(method: String, str_params: String='', _handle: Function=null, a_cache: Boolean = false, a_url: String = ''): void {
			var params: Array = [
				{pn:'querycount', pv: 1},
				{pn:'query1', pv: MODEL_NAME + ';' + method + ';' + str_params}
			];

			jsrHelper(params, _handle, method, a_cache, a_url);
		}
		
		public function removeImage(fileName: String): void {
			oneQuery('removeImage', fileName, null, false, clt_servers.UPLOADJPG_URL);
		}
		
		public function getTemplates(_handle: Function): void {
			oneQuery('getTemplates', '', _handle);
		}
		
		public function getHeads(uid: String, _handle: Function): void {
			oneQuery('getHeads', uid, _handle, true);
		}
		
		public function addHead(uid: String, imageURL: String, maskID: uint, restoreInfo: Object, _handle: Function): void {
			oneQuery('addHead', uid + ';' + imageURL + ';' + maskID + ';' + uif_json.e(restoreInfo)
					, _handle);
		}
		
		public function removeHead(headID: String, _handle: Function): void {
			oneQuery('removeHead', headID, _handle);
		}
		
		public function inUser(uid: String, _handle: Function): void {
			oneQuery('inUser', uid, _handle);
		}
		
		public function banRequest(ban_uid: String, content_id: uint, banType: int, _handle: Function): void {
			oneQuery('banRequest', ban_uid + ';' + content_id + ';' + banType, _handle);
		}
		
		public function collectorImage(path: String, JPGData: ByteArray, _handle: Function=null, uploadServer: String = ''): void {
			var _params: Array = [
				{pn:'querycount', pv: 1},
				{pn:'query1', pv: MODEL_NAME + ';uploadJPGCard;' + 
					this.sroot.externalServer.viewer_id + ';' + path
				}
			];
			
			if (!uploadServer) uploadServer = clt_servers.UPLOADJPG_URL;
			
			var header: URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
			var request: URLRequest = createRequest(_params, uploadServer, URLRequestMethod.GET);
			request.method = URLRequestMethod.POST;
			request.requestHeaders.push(header);
			request.data 	= JPGData;
			
			jsrHelper(_params, _handle, 'collectorImage', false, '', request);
		}
		
		public function imageFromGame(JPGData: ByteArray, name: String, stiker: int=0, _handle: Function=null, mlpImage: Boolean=false, isMyAlbum: Boolean=false): void {
			var _params: Array = [
				{pn:'querycount', pv: 1},
				{pn:'query1', pv: MODEL_NAME + ';uploadFromGame;' + 
					this.sroot.externalServer.viewer_id + ';' + safeString(name) + ';' + stiker + ';' + (mlpImage?1:0) + ';' + (isMyAlbum?1:0)
				}
			];
			
			var header: URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
			var request: URLRequest = createRequest(_params, '', URLRequestMethod.GET);
			request.method = URLRequestMethod.POST;
			request.requestHeaders.push(header);
			request.data 	= JPGData;
			
			jsrHelper(_params, _handle, 'uploadFromGame', false, '', request);
		}
		
		public function getMyAlbum(_handle: Function): void {
			oneQuery('getMyAlbum', sroot.externalServer.viewer_id, _handle);
		}
		
		public function imageFromMyAlbum(JPGData: ByteArray, _handle: Function=null, 
										 name: String='', sender_uid: uint=0, album_id: uint=0, 
										 mlpImage: Boolean=false): void {
			var _params: Array = [
				{pn:'querycount', pv: 1},
				{pn:'query1', pv: clt_myserver.MODEL_NAME + ';uploadFromAlbum;' + 
					sroot.externalServer.viewer_id + ';' + 
					';' + (mlpImage?1:0) + ';' + sender_uid + ';' + album_id
				}
			];
			
			var header: URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
			var request: URLRequest = createRequest(_params, '', URLRequestMethod.GET);
			request.method = URLRequestMethod.POST;
			request.requestHeaders.push(header);
			request.data 	= JPGData;
			
			jsrHelper(_params, _handle, 'uploadFromAlbum', false, '', request);
		}
		
		public function safeString(a_str: String): String {
			return a_str.replace(SAFEREG, '');
		}
		
/*		
		public function renameGame(game_id: String, name: String, stikerID: int, _handle: Function=null): void {
			oneQuery('renameGame', game_id + ';' + safeString(name) + ';' + stikerID, _handle);
		}
*/		

		public function removeGame(uid: String, game_id: String, _handle: Function=null): void {
			oneQuery('removeGame', uid + ';' + game_id, _handle);
		}  

		public function getGameList(params: String, retFunc: Function): void {
			var a_params: Array = [
				{pn:'querycount', pv: 1},
				{pn:'query1', pv: MODEL_NAME + ';gameList;' + params}
			];

			jsrHelper(a_params, function(a_data: Object): void {
				if (a_data.response[0] && (a_data.response[0])) retFunc(a_data.response[0]);
			}, '', false);
		}
		
		public function addCollageToGame(uid: String, fileURL: String, retFunc: Function=null): void {
			oneQuery('addCollageToGame', uid + ';' + fileURL, retFunc);
		}
		
		public function addVotes(game_id: String, uid: String, votes: int, curRate: int, _handle: Function=null): void {
			oneQuery('addVotes', game_id + ';' + uid + ';' + votes + ';' + curRate, _handle);
		}  

		public function getVotes(game_id: String, _handle: Function): void {
			oneQuery('getVotes', game_id, _handle);
		}  

		public function getGame(uid: String, game_id: String, _handle: Function=null): void {
			oneQuery('getGame', uid + ';' + game_id, _handle);
		}

		public function addComment(uid: String, game_id: uint, comment: String, _handle: Function=null): void {
			oneQuery('addComment', uid + ';' + game_id + ';' + encodeURI(safeString(comment)), _handle);
		}

		public function deleteComment(comment_id: String, _handle: Function=null): void {
			oneQuery('deleteComment', comment_id, _handle);
		}
		
		public function listComment(game_id: uint, _handle: Function=null): void {
			oneQuery('listComment', game_id.toString(), _handle);
		}
	}
}